﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisSalaryInfoEditor : System.Web.UI.UserControl
    {
        public object DataItem { get; set; }
        private IHisSalaryBLL hisBLL = new HisSalaryBLL();
		private MoveCommandBLL moveCommandBll = new MoveCommandBLL();
	    private string current_employee_seq = "-1";
		private Main main = new Main();

		protected void Page_Init(object sender, EventArgs e)
		{
			string selectedValue = mov_seq.SelectedValue; 
			mov_seq.DataSource = moveCommandBll.SelectMovmentCombo();
			mov_seq.DataTextField = "move_name";
			mov_seq.DataValueField = "move_seq";
			mov_seq.DataBind();
			mov_seq.SelectedValue = selectedValue;
		}

        protected void Page_Load(object sender, EventArgs e)
		{
			 current_employee_seq = Session["emp_seq"].ToString();
            if (DataItem != null)
            {
                //cbRoyalItemType.ListRoyalItemTypeComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
				{
					mov_seq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "move_seq"));
					/*
                    lblRoyalSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    cmbRoyalItemType.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "RoyalItemTypes.Sequence"));
                    //chkActive.Checked = Convert.ToString(DataBinder.Eval(DataItem, "RecordStatus")) == SystemInfo._ActiveRecordStatus;
                    txtCode.Text = DataBinder.Eval(DataItem, "Code").ToString();
                    txtRoyalName.Text = DataBinder.Eval(DataItem, "Name").ToString();
                    txtRoyalNameShort.Text = DataBinder.Eval(DataItem, "NameShort").ToString();
                     */
				}
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            } 
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
	        // hisBLL.UpdateHisSalary(
	        var hisSalaryInfo = new HisSalaryInfo();
	        hisSalaryInfo.sal_his_seq = String.IsNullOrEmpty(sal_his_seq.Value) ? -1 : Convert.ToInt32(sal_his_seq.Value);
	        hisSalaryInfo.emp_seq = Convert.ToInt32(current_employee_seq);
	        hisSalaryInfo.start_date = start_date.GetDateNullableValue();
	        hisSalaryInfo.end_date = end_date.GetDateNullableValue();
	        hisSalaryInfo.cmd_ref_no = cmd_ref_no.Text;
	        hisSalaryInfo.cmd_name = cmd_name.Text;
	        try
	        {
		        hisSalaryInfo.cmd_date = cmd_date.GetDateNullableValue();
	        }
	        catch
	        {
		        hisSalaryInfo.cmd_date = null;
	        }
	        /*hisSalaryInfo.pos_ref = pos_ref.Text;********/
	        hisSalaryInfo.pos_name = pos_name.Text;
	        hisSalaryInfo.class_seq = -1;
	        hisSalaryInfo.class_name = class_name.Text;
	        hisSalaryInfo.job_site_seq = -1;
	        hisSalaryInfo.job_site_name = job_site_name.Text;
	        hisSalaryInfo.salary_new = String.IsNullOrEmpty(salary_new.Text) ? 0 : Convert.ToDouble(salary_new.Text);
	        hisSalaryInfo.salary_old = String.IsNullOrEmpty(salary_old.Text) ? 0 : Convert.ToDouble(salary_old.Text);
	        hisSalaryInfo.salary_extra = String.IsNullOrEmpty(salary_extra.Text) ? 0 : Convert.ToDouble(salary_extra.Text);
	        hisSalaryInfo.salary_extra_seq = -1;
	        hisSalaryInfo.total_score = String.IsNullOrEmpty(total_score.Text) ? 0 : Convert.ToInt32(total_score.Text);
	        hisSalaryInfo.move_seq = mov_seq.SelectedValue != null ? Convert.ToInt32(mov_seq.SelectedValue) : -1;
	        hisSalaryInfo.move_name = mov_seq.Text;
	        hisSalaryInfo.remarks = remarks.Text;
	        hisSalaryInfo.record_status = "U";
	        hisSalaryInfo.updated_by = -1;
	        hisSalaryInfo.updated_when = DateTime.Now;
	        hisSalaryInfo.created_when = DateTime.Now;
	        hisSalaryInfo.created_by = -1;
	        hisBLL.InsertHisSalary(hisSalaryInfo);
	        main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

	    protected void btnUpdate_Click(object sender, EventArgs e)
        {
			  
            var hisSalaryInfo = new HisSalaryInfo();
            hisSalaryInfo.sal_his_seq = Convert.ToInt32(sal_his_seq.Value);
            hisSalaryInfo.emp_seq = Convert.ToInt32(emp_seq.Value);
            hisSalaryInfo.start_date = start_date.GetDateNullableValue();
            hisSalaryInfo.end_date = end_date.GetDateNullableValue();
            hisSalaryInfo.cmd_ref_no = cmd_ref_no.Text;
            hisSalaryInfo.cmd_name = cmd_name.Text;
            try
            {
                hisSalaryInfo.cmd_date = cmd_date.GetDateNullableValue();
            }
            catch
            {
                hisSalaryInfo.cmd_date = null;
            }
            /*hisSalaryInfo.pos_ref = pos_ref.Text;*********/
            hisSalaryInfo.pos_name = pos_name.Text;
            hisSalaryInfo.class_seq = -1;
            hisSalaryInfo.class_name = class_name.Text;
            hisSalaryInfo.job_site_seq = -1;
            hisSalaryInfo.job_site_name = job_site_name.Text;
            hisSalaryInfo.salary_new = String.IsNullOrEmpty(salary_new.Text) ? 0 : Convert.ToDouble(salary_new.Text);
            hisSalaryInfo.salary_old = String.IsNullOrEmpty(salary_old.Text) ? 0 : Convert.ToDouble(salary_old.Text);
            hisSalaryInfo.salary_extra = String.IsNullOrEmpty(salary_extra.Text) ? 0 : Convert.ToDouble(salary_extra.Text);
            hisSalaryInfo.salary_extra_seq = -1;
			hisSalaryInfo.total_score = String.IsNullOrEmpty(total_score.Text) ? 0 : Convert.ToInt32(total_score.Text);
			hisSalaryInfo.move_seq = mov_seq.SelectedValue != null ? Convert.ToInt32(mov_seq.SelectedValue) : -1;
			hisSalaryInfo.move_name = mov_seq.Text;
            hisSalaryInfo.remarks = remarks.Text;
            hisSalaryInfo.record_status = "U";
            hisSalaryInfo.updated_by = -1;
            hisSalaryInfo.updated_when = DateTime.Now;
            hisSalaryInfo.created_when = DateTime.Now;
            hisSalaryInfo.created_by = -1;

			hisBLL.UpdateHisSalary(hisSalaryInfo);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
			  
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}